package com.yanqu.road.server.manager.activity.chuhan;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.activity.chuhan.ChuHanConfig;
import com.yanqu.road.logic.activity.chuhan.ChuHanMapLogic;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.activity.chuhan.activity.CrossChuHanActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossChuHanConfigMgr extends TempCrossMgr {
    private static Map<Integer, ChuHanConfig> configMap = new ConcurrentHashMap<>();

    @Override
    public boolean removeExpireData() throws Exception {
        return false;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        Map<Integer, Map<Integer, CrossChuHanActivity>> groupMap = CrossChuHanMgr.getActivityGroupMap();
        for (Map.Entry<Integer, Map<Integer, CrossChuHanActivity>> entry : groupMap.entrySet()) {
            int activityId = entry.getKey();
            ChuHanConfig config = reload(activityId);

            for (CrossChuHanActivity activity : entry.getValue().values()) {
                activity.loadConfigFromDb(config.getActivityInfo());
            }
        }
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static ChuHanConfig reload(int activityId) {
        ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
        if (activityInfo == null) {
            return null;
        }

        if (activityInfo.getType() != eActivityType.ChuHanZB.getValue()) {
            return null;
        }

        List<Integer> activityIdList = new ArrayList<>();
        activityIdList.add(activityId);
        Map<String, ActivityConfig> activityConfigMap = ActivityBussiness.getActivityConfigMap(activityIdList).get(activityId);
        ChuHanConfig config = new ChuHanConfig(activityInfo, activityConfigMap, ChuHanMapLogic.getNpcEventPosNumConfig(), ChuHanMapLogic.getMapGraph());
        configMap.put(activityId, config);
        return config;
    }

    public static ChuHanConfig getConfig(int activityId) {
        return configMap.get(activityId);
    }
}
